package com.zy.web.demo.duoxiancheng.demo27;


import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author <a href="mailto:zhouyong@cqyyt.com">zy</a>
 * @version 1.0
 * @date 2020/8/28 14:19
 * ReentrantLock的生产者/消费者：一对一交替打印
 */
public class ShenChan27 {

    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
    private boolean zy = false;

    public void set(){
        lock.lock();
        try {
            while ( zy == true ){
                condition.await();
            }
            System.out.println("打印黑心");
            zy = true;
            condition.signalAll();
        }catch (InterruptedException e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public void get(){
        lock.lock();
        try {
            while ( zy == false ){
                condition.await();
            }
            System.out.println("打印白心");
            zy = false;
            condition.signalAll();
        }catch (InterruptedException e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

}
